import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:xianyu_school/utils/url.dart';
import 'package:xianyu_school/xianyuApp/entity/goods/goods.dart';
import 'package:xianyu_school/xianyuApp/home/List/homeGoodItem.dart';
import 'Widgets/BallPulseIndicator.dart';

class myPostPage extends StatelessWidget{

  List<goods> gs = [];
  String url = urls().url;

  @override
  Widget build(BuildContext context) {

    Future getgoods() async {
      try{
        SharedPreferences pref = await SharedPreferences.getInstance();
        String username = pref.getString("username");
        Response re = await Dio().get(url+"/users/getMyPost",queryParameters: {"username":username});
        print(re.data.toString());
        return re;
      }
      catch(e){
        return ;
      }
    }

    return Scaffold(
      appBar: CupertinoNavigationBar(
        backgroundColor: Colors.yellow,
        middle: Text('我发布的'),
      ),
      body:FutureBuilder(
        future: getgoods(),
        builder: (context,snapshot){
          if(snapshot.hasData){
            var responseJson = json.decode(snapshot.data.toString());
            responseJson.forEach((data) {
              gs.add(goods.fromJson(data));
            });
            return CustomScrollView(
              slivers: [
                // 商品列表
                SliverPadding(
                  padding: EdgeInsets.fromLTRB(10, 5, 10, 10),
                  sliver: SliverStaggeredGrid.countBuilder(

                    crossAxisCount: 1,
                    crossAxisSpacing: 10,
                    mainAxisSpacing: 10,
                    itemCount: gs.length,
                    itemBuilder: (BuildContext context, int index) {
                      return FHHomeGoodItem(model: gs[index],);
                    },
                    staggeredTileBuilder: (int index) {
                      return StaggeredTile.fit(2);
                    },
                  ),
                ),
              ],
            );
          }
          else return BallPulseIndicator(
              color: Colors.white
          );
        },
      ),
    );
  }
}